package com.beerandjesus.sportofkings.handicapper.strategy;

import com.beerandjesus.sportofkings.handicapper.model.KeyTrainerStatistics;
import com.beerandjesus.sportofkings.handicapper.model.TrainerJockeyOwner;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.KeyTrainerStatisticsCategory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Date: 2/3/14
 * Time: 9:38 PM
 */
@Component
public class KeyTrainerStatisticsResolver {
    private static final Double TWENTY = 20.0;

    public List<KeyTrainerStatistics> resolve(Horse horse) {
        TrainerJockeyOwner tjo = horse.getTrainerJockeyOwner();
        if (tjo == null) {
            return null;
        }
        List<KeyTrainerStatistics> keyTrainerStatistics = tjo.getKeyTrainerStatistics();
        if (keyTrainerStatistics == null) {
            return null;
        }
        List<KeyTrainerStatistics> filteredList = new ArrayList<KeyTrainerStatistics>();
        filteredList.addAll(keyTrainerStatistics);
        CollectionUtils.filter(filteredList, new Predicate() {
            public boolean evaluate(Object o) {
                KeyTrainerStatistics stats = (KeyTrainerStatistics) o;
                String winPercentage = stats.getWinPercentage();
                return !stats.getCategory().equals(KeyTrainerStatisticsCategory.STAT_UNKNOWN)
                        && TWENTY.compareTo(getDouble(winPercentage)) < 0;
            }
        });
        return filteredList;
    }

    private double getDouble(String winPercentage) {
        try {
            return Double.parseDouble(winPercentage);
        } catch (Exception e) {
            return 0d;
        }
    }
}
